package com.qfedu.zf.index.service.impl;

import cn.hutool.core.convert.NumberWithFormat;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.zf.common.config.SystemConfig;
import com.qfedu.zf.common.utils.JwtUtil;
import com.qfedu.zf.common.vo.R;
import com.qfedu.zf.index.entity.Browse;
import com.qfedu.zf.index.entity.Listings;
import com.qfedu.zf.index.mapper.BrowseMapper;
import com.qfedu.zf.index.mapper.ListingsMapper;
import com.qfedu.zf.index.service.ListingsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 房源表 服务实现类
 * </p>
 *
 * @author wgz
 * @since 2024-06-15
 */
@Service
public class ListingsServiceImpl extends ServiceImpl<ListingsMapper, Listings> implements ListingsService {

    @Autowired
    ListingsMapper listingsMapper;

    @Autowired
    BrowseMapper browseMapper;

    @Override
    public R selectByCityAll(Map map) {
        if (map.get("pagSize") == null || map.get("pagNum") == null){
            return R.fail("参数错误");
        }
        Integer pagSize = Integer.parseInt((String) map.get("pagSize"));
        Integer pagNum = Integer.parseInt((String) map.get("pagNum"));
        Page<Listings> page = new Page(pagNum, pagSize);

        //条件查询
        QueryWrapper<Listings> queryWrapper = NewQueryWrapper(map);

        IPage<Listings> listings =  listingsMapper.selectPage(page, queryWrapper);
        List<Listings> listingsList = listings.getRecords();
        if (listingsList == null || listingsList.size() == 0){
            return R.fail("未查到更多房源");
        }
        return R.ok(listingsList);
    }

    @Override
    public R info(Map<String, Object> map, HttpServletRequest request) {
        if (map.get("listingId") == null || map.get("listingId") == ""){
            return R.fail("参数错误");
        }
        if (request.getHeader(SystemConfig.HEADER_TOKEN) == null){
            System.out.println("token = " + request.getHeader(SystemConfig.HEADER_TOKEN));
            return R.fail("未登录");
        }
        Listings listings = listingsMapper.selectById((Serializable) map.get("listingId"));
        if (listings == null){
            return R.fail("未查到该房源");
        }

        Browse browse = new Browse();
        String header = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer userId = ((NumberWithFormat) JwtUtil.parseToken(header,"id")).intValue();
        browse.setUserId(userId);
        browse.setListingId(listings.getListingId());
        browse.setCreateDate(new Date());
        if (listings.getPopularity() == null){
            listings.setPopularity(1);
        }else {
            listings.setPopularity(listings.getPopularity() + 1);
        }
        browseMapper.insert(browse);
        listingsMapper.updateById(listings);

        return R.ok(listings);
    }

    /**
     * 添加查询条件
     * @param map
     * @return
     */
    public QueryWrapper<Listings> NewQueryWrapper(Map map){
        QueryWrapper<Listings> queryWrapper = new QueryWrapper<>();

        if (map.get("cityId") != null && map.get("cityId") != ""){
            queryWrapper.eq("city_id", map.get("cityId"));
        }

        if (map.get("orderByAsc") != null && map.get("orderByAsc") != ""){//排序
            queryWrapper.orderByAsc((String) map.get("orderByAsc"));
        }
        if (map.get("orderByDesc") != null && map.get("orderByDesc") != ""){//排序
            queryWrapper.orderByDesc((String) map.get("orderByDesc"));
        }

        //搜索条件
        if (map.get("keywords") != null && map.get("keywords") != ""){
            queryWrapper.like("title", map.get("keywords"))
                    .or().like("description", map.get("keywords"))
                    .or().like("advantage", map.get("keywords"))
                    .or().like("house_type", map.get("keywords"))
                    .or().like("location", map.get("keywords"))
                    .or().like("type", map.get("keywords"))
                    .or().like("pay", map.get("keywords"))
                    .or().like("floor", map.get("keywords"));
            try {
                queryWrapper.or().like("area", map.get("keywords"));
                queryWrapper.or().like("price", Integer.parseInt((String) map.get("keywords")));
            } catch (Exception ignored) {}
        }

        //租金条件
        if (map.get("priceA") != null && map.get("priceB") != null){
            if (Integer.parseInt((String) map.get("priceA")) != 0 || Integer.parseInt((String) map.get("priceB")) != 0){
                queryWrapper.between("price", Integer.parseInt((String) map.get("priceA")), Integer.parseInt((String) map.get("priceB")));
            }
        }

        //type
        if (map.get("type") != null && map.get("type") != ""){
            queryWrapper.eq("type", map.get("type"));
        }

        //houseType
        if (map.get("houseType") != null && map.get("houseType") != ""){
            queryWrapper.like("house_type", map.get("houseType"));
        }

        //亮点条件
        if(map.get("zhuangxiu") != null && map.get("zhuangxiu") != ""){
            queryWrapper.like("advantage", map.get("zhuangxiu"));
        }
        if(map.get("weishengjian") != null && map.get("weishengjian") != ""){
            queryWrapper.or().like("advantage", map.get("weishengjian"));
        }
        if(map.get("chufang") != null && map.get("chufang") != ""){
            queryWrapper.or().like("advantage", map.get("chufang"));
        }
        if(map.get("chaoxiang") != null && map.get("chaoxiang") != ""){
            queryWrapper.or().like("advantage", map.get("chaoxiang"));
        }
        if(map.get("dianti") != null && map.get("dianti") != ""){
            queryWrapper.or().like("advantage", map.get("dianti"));
        }

        //location
        if(map.get("location") != null && map.get("location") != ""){
            queryWrapper.or().like("location", map.get("location"));
        }

        return queryWrapper;
    }
}
